﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using haptic.src.engine.design;
using Microsoft.Xna.Framework.Content;

namespace haptic.src.engine.animation
{
    [Serializable]
    [XmlRootAttribute("automate")]
    public class Automate : IInstanciable<AutomateInstance>, IXmlSerializable
    {
        #region Constructors
        public Automate(){}
        public Automate(string sFilePath)
        {
            m_sFilePath = sFilePath;
        }
        public Automate(SerializationInfo oInfo, StreamingContext oContext)
        {
            //m_oEntities = (List<Entity>)oInfo.GetValue("entities", typeof(List<Entity>));
        }
        public AutomateInstance CreateInstance()
        {
            return new AutomateInstance(this);
        }
        public void GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
        {
            oInfo.AddValue("hey", "hey");
            //oInfo.AddValue("entities", m_oEntities);
        }
        #endregion

        #region Edit
        public void AddState(State oState)
        {
            Debug.Assert(oState.m_sName != null);
            Debug.Assert(oState.m_sName != "");
            m_oStates.Add(oState.m_sName, oState);
      }
        #endregion

        #region Load
        public void Load(ContentManager oContent)
        {
            foreach ( State oState in m_oStates.Values)
            {
                oState.Load(oContent);
            }
        }
        #endregion

        #region Accessors
        public State GetState(string sName)
        {
            if (sName == null)
                return null;
            else
                return (State)m_oStates[sName];
        }
        public State GetDefaultState()
        {
            if (m_sDefaultState != null)
                return (State)m_oStates[m_sDefaultState];
            else
                return null;
        }
        #endregion

        #region Serialization
        public XmlSchema GetSchema()
        {
            return null;
        }
        public void WriteXml(XmlWriter w)
        {
            // default state
            w.WriteAttributeString("default-state", m_sDefaultState);

            // states
            foreach(State oState in m_oStates.Values)
            {
                XmlSerializer oSrlz = new XmlSerializer(typeof(State));
                oSrlz.Serialize(w, oState);
            }
        }
        public void ReadXml(XmlReader r)
        {
            // default state
            m_sDefaultState = r.GetAttribute("default-state");

            // states
            m_oStates.Clear();
            while (r.ReadToFollowing("state"))
            {
                XmlSerializer oSrlz = new XmlSerializer(typeof(State));
                State oState = (State)oSrlz.Deserialize(r);
                m_oStates.Add(oState.m_sName, oState);
            }
        }
        public static Automate Deserialize(Scene oGame, string sFilePath)
        {
            Automate oAutomate = null;
            XmlSerializer oSerializer = new XmlSerializer(typeof(Automate));
            FileStream oFile = new FileStream(sFilePath, FileMode.Open);
            if (oFile != null)
            {
                oAutomate = (Automate)oSerializer.Deserialize(oFile);
                oAutomate.m_sFilePath = sFilePath;
                oFile.Close();
            }

            return oAutomate;
        }
        public void Serialize()
        {
            XmlSerializer oSerializer = new XmlSerializer(typeof(Automate));
            FileStream oFile = new FileStream(m_sFilePath, FileMode.Create);
            oSerializer.Serialize(oFile, this);
            oFile.Close();
        }
        #endregion

        #region Attributes
        private Hashtable m_oStates = new Hashtable();
        private Hashtable m_oTransitions = new Hashtable();
        public List<State> oXMLStates 
        { 
            get 
            {
                List<State> oStates = new List<State>();
                foreach (State oState in m_oStates.Values)
                {
                    oStates.Add(oState);
                }
                return oStates;
            }
        }
        public string m_sDefaultState;
        private string m_sFilePath;
        public string sFilePath { get { return m_sFilePath; } }
        #endregion
    }
}
